var express = require('express');
var router = express.Router();
var Notice = require('../db/notice.js');
var Message = require('../db/message.js');
var User = require('../db/user.js');
const fs = require('fs');

//用户接收私信
router.post('/getTalk', function (req, res, next) {
  Message.findOne({ uid: req.cookies.userID }, function (err, data) {
    if (err) { console.log(err); }
    if (data == null) {
      res.send("");
    } else {
      let MsList = [];
      for (let i = 0; i < data.messageBody.length; i++) {
        if (MsList.length != 0) {
          for (let j = 0; j < MsList.length; j++) {
            if (MsList[j].uid == data.messageBody[i].uid) {
              let content = data.messageBody[i].content;
              MsList[j].content.push(content);
            } else {
              let list = {
                uid: data.messageBody[i].uid,
                name: data.messageBody[i].name,
                content: [data.messageBody[i].content],
              }
              MsList.push(list);
            }
          }
        } else {
          let list = {
            uid: data.messageBody[i].uid,
            name: data.messageBody[i].name,
            head: "http://localhost:3000/" + data.messageBody[i].head,
            content: [data.messageBody[i].content],
          }
          MsList.push(list);
        }

      }
      // console.log(MsList);
      res.send(MsList);
    }

  })
})

//用户获取未读私信数量
router.post('/getTalkNum', function (req, res, next) {
  Message.findOne({ uid: req.cookies.userID }, function (err, data) {
    if (err) { console.log(err); }
    if (data == null) {
      res.json({
        num: 0,
      });
    } else {
      res.json({
        num: data.messageBody.length,
      });
    }
  })
})

//给用户发私信
router.post('/doTalk', function (req, res, next) {
  // console.log(req.body);
  User.findOne({ uid: req.cookies.userID }, function (err, data) {
    if (err) { console.log(err); }
    Message.findOne({ uid: req.body.goalUserId }, function (err, list) {
      if (err) { console.log(err); }
      if (list == null) {
        let message = Message({
          uid: req.body.goalUserId,
          messageBody: [{
            uid: req.cookies.userID,
            name: data.uname,
            head: data.img_head,
            content: req.body.MsContent,
            sendTime: req.body.sendTime
          }]
        });
        message.save(function (err, data) {
          if (err) { console.log(err); }
          res.send("已发送");
        })
      } else {
        Message.updateOne({ uid: req.body.goalUserId }, { $addToSet: { messageBody: { uid: req.cookies.userID, name: data.uname, head: data.img_head, content: req.body.MsContent, sendTime: req.body.sendTime } } }, function (err) {
          if (err) { console.log(err); }
          res.send("已发送");
        })
      }
    })
  })
})

//消除未读通告
router.post('/removeNotice', function (req, res, next) {
  Notice.updateOne({ time: req.body.time }, { $addToSet: { readedList: req.cookies.userID } }, function (err) {
    if (err) { console.log(err); }
    res.send("1");
  })
})

//获取未读通告
router.post('/getNotice', function (req, res, next) {
  Notice.find({}, function (err, data) {
    if (err) { console.log(err); }
    // console.log(data);
    let notRead = [];
    for (let i = 0; i < data.length; i++) {
      if (data[i].readedList.includes(req.cookies.userID) == true) {
        notRead.push({
          type: true,
          time: data[i].time,
          content: data[i].content,
        });
      } else {
        notRead.push({
          type: false,
          time: data[i].time,
          content: data[i].content,
        });
        // console.log(2);
      }
    }
    res.send(notRead);
  })
})

//获取未读通告数量
router.post('/getNoticeNum', function (req, res, next) {
  Notice.find({}, function (err, data) {
    if (err) { console.log(err); }
    let readed = 0;
    for (let i = 0; i < data.length; i++) {
      if (data[i].readedList.includes(req.cookies.userID) == true) {
        readed = readed + 1;
      }
    }
    let notRead = data.length - readed;
    res.json({ notRead: notRead });
  })
})

module.exports = router;
